<template>
  <div class="code-highlighter">
    <div class="code-header">
      <span class="language-label">{{ language || 'plaintext' }}</span>
      <span class="copy-button" @click="copyCode">复制</span>
    </div>
    <pre><code ref="codeBlock" :class="language">{{ code }}</code></pre>
  </div>
</template>

<script>
import { ref, onMounted, watch } from 'vue'
import hljs from 'highlight.js'
import 'highlight.js/styles/atom-one-dark.css'

export default {
  name: 'CodeHighlighter',
  props: {
    code: {
      type: String,
      required: true
    },
    language: {
      type: String,
      default: ''
    }
  },
  setup(props) {
    const codeBlock = ref(null)
    
    const highlightCode = () => {
      if (codeBlock.value) {
        hljs.highlightElement(codeBlock.value)
      }
    }
    
    const copyCode = () => {
      navigator.clipboard.writeText(props.code).then(() => {
        // 使用Toast或者ElMessage替代alert，提供更好的用户体验
        const originalText = document.querySelector('.copy-button').textContent;
        document.querySelector('.copy-button').textContent = '复制成功!';
        
        setTimeout(() => {
          document.querySelector('.copy-button').textContent = originalText;
        }, 1500);
      }).catch(err => {
        console.error('复制失败:', err)
      })
    }
    
    onMounted(() => {
      highlightCode()
    })
    
    watch(() => props.code, () => {
      highlightCode()
    })
    
    return {
      codeBlock,
      copyCode
    }
  }
}
</script>

<style scoped>
.code-highlighter {
  margin: 1.5em 0;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #e4e7ed;
}

.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 16px;
  background-color: #f6f8fa;
  border-bottom: 1px solid #e4e7ed;
  font-size: 13px;
}

.language-label {
  color: #606266;
  font-family: Consolas, Monaco, monospace;
  text-transform: lowercase;
  font-weight: 600;
}

.copy-button {
  cursor: pointer;
  color: #606266;
  padding: 3px 10px;
  border-radius: 4px;
  background-color: #eef2f5;
  transition: all 0.2s;
  font-size: 12px;
}

.copy-button:hover {
  background-color: #e0e4ea;
  color: #333;
}

.code-highlighter pre {
  margin: 0;
  padding: 16px;
  border-radius: 0 0 6px 6px;
  overflow-x: auto;
  background-color: #282c34; /* Dark background */
  position: relative;
}

.code-highlighter code {
  font-family: Consolas, 'SFMono-Regular', 'Liberation Mono', Menlo, monospace;
  font-size: 14px;
  line-height: 1.5;
  display: block;
}

/* 代码行样式 */
.code-highlighter code > span {
  display: block;
  line-height: 1.6;
  position: relative;
  padding-left: 2.5em;
  border-left: 3px solid transparent;
}

/* 增加行号样式 */
.code-highlighter pre {
  counter-reset: line;
}

.code-highlighter code span::before {
  counter-increment: line;
  content: counter(line);
  position: absolute;
  left: 0;
  color: #636d83;
  text-align: right;
  width: 2em;
  padding-right: 0.5em;
  user-select: none;
  opacity: 0.7;
  border-right: 1px solid rgba(255, 255, 255, 0.1);
}

/* 悬停效果 */
.code-highlighter code span:hover {
  background-color: rgba(255, 255, 255, 0.07);
  border-left: 3px solid #409EFF;
}

/* 高亮特定语言的关键字 */
:deep(.hljs-keyword) {
  color: #cc7832; /* Java keywords */
  font-weight: bold;
}

:deep(.hljs-string) {
  color: #6a8759; /* String literals */
}

:deep(.hljs-comment) {
  color: #808080;
  font-style: italic;
}

:deep(.hljs-function) .hljs-title,
:deep(.hljs-class) .hljs-title {
  color: #ffc66d;
}

:deep(.hljs-type) {
  color: #ffc66d;
}

:deep(.hljs-number) {
  color: #6897bb;
}
</style> 